package org.jeecg.modules.AccessoriesCenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.AccessoriesCenter.entity.ButtonDaochuEntity;
import org.jeecg.modules.AccessoriesCenter.entity.ButtonEntity;
import org.jeecg.modules.AccessoriesCenter.service.ButtonManageService;
import org.jeecg.modules.zhoupackage.pojo.MlccFabricInfo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/ButtonManage")
public class ButtonManageController {

    @Autowired
    ButtonManageService service;

    @GetMapping("/Buttonlist")
    public Result<?> Buttonlist(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "status", defaultValue = "10") Integer status,
            String buttonsName,
            String buttonsCode,
            String categoryId
    ){
        Page<ButtonEntity>  page = new Page<ButtonEntity>(pageNo, pageSize);
        IPage<ButtonEntity> pageList=service.getButtonlist(page,status,buttonsName,buttonsCode,categoryId);
        return Result.ok(pageList);
    }

    @GetMapping("/updateButtonstatus")
    public Result<?> updateButtonstatus(
            @RequestParam(name = "id") int id,
            @RequestParam(name = "status") int status
    ){
        int count=service.updateButtonstatus(id,status);
        if(count>0){
            return Result.ok("操作成功");
        }else {
            return Result.error("操作失败");
        }
    }


    @GetMapping("/getButtonbyId")
    public Result<?> getButtonbyId(
            @RequestParam(name = "id") int id
    ){
        ButtonEntity entity=service.getButtonbyId(id);
        return Result.ok(entity);
    }

    @PostMapping("/addButton")
    public Result<?> addButton(
           @RequestBody ButtonEntity entity
    ){
        try {
        //查询纽扣编号是否已经存在
        String cunzai=service.getButtoncodebycode(entity.getButtonsCode());
        if(cunzai!=null){
            return Result.error("该编号已存在,请修改");
        }
        int count=service.addButton(entity);
        if(count>0){
            return Result.ok("新增成功");
        }else {
            return Result.error("新增失败");
        }

        }catch (Exception e){
            e.printStackTrace();
            return Result.error("新增失败");
        }
    }

    @PostMapping("/updateButton")
    public Result<?> updateButton(
            @RequestBody ButtonEntity entity
    ){
        try {
            int count=service.updateButton(entity);
        if(count>0){
            return Result.ok("操作成功");
        }else {
            return Result.error("操作失败");
        }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }


    /**
     * 查询所有的 纽扣品类
     */
    @GetMapping("/getallButtonCategory")
    public Result<?> getallButtonCategory(){
        List<ButtonEntity> list=service.getallButtonCategory();
        return Result.ok(list);
    }


    /**
     * 批量上下架 批量膝盖状态
     */
    @PostMapping("/piliangUpdatestatus")
    public Result<?> piliangUpdatestatus(
            @RequestBody    HashMap<String,Object> map

    ){
           try {
               List<Integer> ids=(List<Integer>) map.get("ids");
               int status=(Integer) map.get("status");

                if(ids!=null){
                    service.piliangUpdatestatus( ids,status);
                }
               return Result.ok("操作成功");
           }catch (Exception e){
               e.printStackTrace();
               return Result.error("操作失败");
           }
    }


    /**
     * 导出
     */
    @GetMapping("/downloadButton")
    public ModelAndView downloadButton(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "status", defaultValue = "10") Integer status,

            String buttonsCode,
            String categoryId
                                     ) {
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//	          List<JeecgDemo> pageList = jeecgDemoService.list();
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME,"纽扣列表");
        //注解对象Class
        mv.addObject(NormalExcelConstants.CLASS, ButtonDaochuEntity.class);
        //自定义表格参数
        mv.addObject(NormalExcelConstants.PARAMS,new ExportParams("纽扣列表", "纽扣列表"));
        //导出数据列表
        mv.addObject(NormalExcelConstants.DATA_LIST,data(status,buttonsCode,categoryId));
        return mv;
    }

    public List<ButtonDaochuEntity> data(Integer status, String buttonsCode, String categoryId) {
        List<ButtonDaochuEntity> list = new ArrayList<ButtonDaochuEntity>();
        list=service.getButtonDaochulist(status,buttonsCode,categoryId);
        return list;
    }




    /**
     * 导入纽扣excel
     *
     * @param file  上传的Excel文件
     * @param group 面料组ID
     * @return
     */
    @PostMapping("/importExcel")
    public Result<?> importExcel(MultipartFile file) throws Exception {

        ImportParams params = new ImportParams();
        //表格标题所在行，计数从0开始
        params.setTitleRows(1);
        //head头部所在行，计数从0开始
        params.setHeadRows(1);
        //表格sheet数量
        //params.setSheetNum(9);
        // 最好不要设置为true，否则导入一次excel服务器会重启一次，原因不明
        //params.setNeedSave(true);

        InputStream inputStream = file.getInputStream();
        List<ButtonDaochuEntity> list = ExcelImportUtil.importExcel(inputStream, ButtonDaochuEntity.class, params);
        int result = service.importExcel(list);
        if (result > 0) {
            return Result.ok("导入成功");
        } else {
            return Result.ok("导入失败");
        }
    }
}
